<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/5/22
 * Time: 9:40
 * @copyright: ©2024 永州微 聚商网络科技有限公司
 * @link: https://www.weijuyunKe.com

 */
namespace app\admin\controller;

use app\Request;
use app\admin\model\HomeMenu as  HomeMenuModel;
use app\services\wechat\Offiaccount;

class  HomeMenu
{
    public function addMenu(Request $request){
        $param = $request->param();
        $data= [
            'name' => $param['name'],
            'pid' => $param['pid'],
            'type' => $param['type'],
            'create_time' =>  time(),
            'sort' => $param['sort'],
        ];
        if (isset($param['url'])){
            $data['url'] =  $param['url'];
        }

        if (isset($param['key'])){
            $data['key'] =  $param['key'];
        }
        $homeMenuModel  =  new HomeMenuModel();
        if ($param['pid']> 0){
            $pidMenu = $homeMenuModel->where(['id'=> $param['pid']])->find();
            if ($pidMenu['pid'] > 0){
                return adminError('最多添加二级菜单');
            }
        }else{
            $pidCount = $homeMenuModel->where(['id'=> $param['pid']])->count();
            if ($pidCount == 3){
                return adminError('一级菜单只能三个');
            }

        }
        $homeMenuModel->insert($data);
        return adminSuccess('添加成功');
    }

    public function editMenu(Request $request){
        $param = $request->param();
        $data= [
            'name' => $param['name'],
            'pid' => $param['pid'],
            'type' => $param['type'],
            'sort' => $param['sort'],
        ];
         if (isset($param['url'])){
                $data['url'] =  $param['url'];
         }
        if (   isset($param['key'])){
            $data['key'] =  $param['key'];
        }
        $homeMenuModel  =  new HomeMenuModel();
        $homeMenuModel->where(['id' => $param['id']])->update($data);
        return  adminSuccess('添加成功');
    }


    public function saveMenu(){
        $homeMenuModel  =  new HomeMenuModel();
        $topMenu = $homeMenuModel->order('sort','asc')->where('pid','=', 0)->select()->toArray();
        $menuData = [];
        if ($topMenu){
           foreach ( $topMenu as $value){
               if ($value['type'] == 1){
                   $menuData[$value['id']]['name'] = $value['name'];
                   $menuData[$value['id']]['sub_button'] =[];
               }elseif ($value['type'] == 2){
                   $menuData[$value['id']] =[
                       'type'=>'click',
                       'name' =>  $value['name'],
                       'key' =>  $value['key'],
                   ];
               }elseif ($value['type'] == 3){
                   $menuData[$value['id']] =[
                       'type'=>'view',
                       'name' =>  $value['name'],
                       'url' =>  $value['url'],
                   ];
               }
           }

           $id = array_column($topMenu,'id');
           $lastMenu = $homeMenuModel->order('sort','asc')->where('pid','in', $id)->select()->toArray();
           $lastMenuData = [];
           foreach ($lastMenu as $lastMenuValue){
               if ($lastMenuValue['type'] == 2){
                   $lastMenuData =[
                       'type'=>'click',
                       'name' =>  $lastMenuValue['name'],
                       'key' =>  $lastMenuValue['key'],
                   ];

               }elseif ($lastMenuValue['type'] == 3){
                   $lastMenuData = [
                       'type'=>'view',
                       'name' =>  $lastMenuValue['name'],
                       'url' =>  $lastMenuValue['url'],
                   ];
               }
               $menuData[$lastMenuValue['pid']]['sub_button'][] = $lastMenuData;
           }
           $menuNewData['button'] = array_values($menuData);
           $offiaccount =  new Offiaccount();
           $menuNewData;;
         $offiaccount->custom_menu($menuNewData);
        }else{
            return adminError('未建菜单');
        }
        return adminSuccess('请求成功');
    }

    public function delMenu(Request $request){
        $param = $request->param();

        $homeMenuModel  =  new HomeMenuModel();
        $homeMenuInfo = $homeMenuModel->where(['pid' => $param['id']])->find();
        if ($homeMenuInfo){
          return  adminError('请删除子菜单');
        }

        $homeMenuModel->where(['id' => $param['id']])->delete();
        return adminSuccess('操作成功');
    }

    public  function menu(Request $request){
        $homeMenuModel  =  new HomeMenuModel();
        $lastMenu = $homeMenuModel->order('sort','asc')->select()->toArray();
        return adminSuccess('菜单列表',$lastMenu);
    }

}